package com.xx.sg.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.xx.sg.own.entity.Camera;
import com.xx.sg.own.service.CameraService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/hkApi")
@Tag(name = "海康平台Api", description = "海康平台Api")
public class HkController {

    @Value("${hk.host}")
    private String HK_HOST;
    @Value("${hk.key}")
    private String HK_KEY;
    @Value("${hk.secret}")
    private String HK_SECRET;
    @Value("${hk.artemis-path}")
    private String ARTEMIS_PATH;

    @Autowired
    private CameraService cameraService;

    private final String CAMERA_LIST_URL = "/api/resource/v2/camera/search";
    private final String CAMERA_STATUS_URL = "/api/nms/v1/online/camera/get";
    private final String REGIONS_LIST_URL = "/api/resource/v1/regions";
    private final String CAMERA_PREVIEW_URL = "/api/video/v2/cameras/previewURLs";
    private final String CAMERA_CONTROL_URL = "/api/video/v1/ptzs/controlling";

    @PostMapping("/searchCamera")
    @Operation(summary = "查询监控点列表", description = "查询监控点列表")
    public JSONObject searchCamera(@RequestBody JSONObject jsonObject) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(HK_HOST);
        config.setAppKey(HK_KEY);
        config.setAppSecret(HK_SECRET);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", ARTEMIS_PATH + CAMERA_LIST_URL);
            }
        };
        String response = ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toString(),
                null, null, "application/json;charset=UTF-8");
        return JSONObject.parseObject(response);
    }

    @PostMapping("/cameraStatus")
    @Operation(summary = "获取监控点在线状态", description = "获取监控点在线状态")
    public JSONObject cameraStatus(@RequestBody JSONObject jsonObject) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(HK_HOST);
        config.setAppKey(HK_KEY);
        config.setAppSecret(HK_SECRET);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", ARTEMIS_PATH + CAMERA_STATUS_URL);
            }
        };
        String response = ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toString(),
                null, null, "application/json;charset=UTF-8");
        return JSONObject.parseObject(response);
    }

    @PostMapping("/regions")
    @Operation(summary = "分页获取区域列表", description = "分页获取区域列表")
    public JSONObject regions(@RequestBody JSONObject jsonObject) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(HK_HOST);
        config.setAppKey(HK_KEY);
        config.setAppSecret(HK_SECRET);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", ARTEMIS_PATH + REGIONS_LIST_URL);
            }
        };
        String response = ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toString(),
                null, null, "application/json;charset=UTF-8");
        return JSONObject.parseObject(response);
    }

    @PostMapping("/previewURLs")
    @Operation(summary = "获取监控点预览取流URL", description = "获取监控点预览取流URL")
    public JSONObject previewURLs(@RequestBody JSONObject jsonObject) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(HK_HOST);
        config.setAppKey(HK_KEY);
        config.setAppSecret(HK_SECRET);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", ARTEMIS_PATH + CAMERA_PREVIEW_URL);
            }
        };
        String response = ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toString(),
                null, null, "application/json;charset=UTF-8");
        return JSONObject.parseObject(response);
    }

    @PostMapping("/cameraControl")
    @Operation(summary = "监控点编号进行云台操作", description = "监控点编号进行云台操作")
    public JSONObject cameraControl(@RequestBody JSONObject jsonObject) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(HK_HOST);
        config.setAppKey(HK_KEY);
        config.setAppSecret(HK_SECRET);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", ARTEMIS_PATH + CAMERA_CONTROL_URL);
            }
        };
        String response = ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toString(),
                null, null, "application/json;charset=UTF-8");
        return JSONObject.parseObject(response);
    }

    @PostMapping("/synCameraList")
    @Operation(summary = "同步监控列表", description = "同步监控列表")
    public JSONObject synCameraList(@RequestBody JSONObject params) throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost(HK_HOST);
        config.setAppKey(HK_KEY);
        config.setAppSecret(HK_SECRET);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", ARTEMIS_PATH + CAMERA_LIST_URL);
            }
        };

        String response = ArtemisHttpUtil.doPostStringArtemis(config, path, params.toString(),
                null, null, "application/json;charset=UTF-8");
        JSONObject jsonObject = JSONObject.parseObject(response);
        JSONArray dataList = jsonObject.getJSONObject("data").getJSONArray("list");

        for (int i = 0; i < dataList.size(); i++) {
            Camera camera = dataList.getJSONObject(i).toJavaObject(Camera.class);
            Camera cameraDto = cameraService.getCameraByCode(camera.getIndexCode());
            if(cameraDto == null) {
                cameraService.save(camera);
            }else {
                camera.setId(cameraDto.getId());
                cameraService.updateById(camera);
            }
        }
        return new JSONObject();
    }

}
